from rakg.graph_utils import *
from rakg.timer import *
import rakg.graphsim as gsim
import numpy as np
import networkx as nx
from itertools import izip
import sys
from memory_profiler import profile
from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier
from sklearn.externals import joblib
from sklearn import cross_validation

def main():
    test3()
    
def test3():
    print 'Started...'
    t = Timerx(True)
    path = "/Users/rockyrock/Desktop/all.txt"
    G = read_graph(path)
    U = build_U(G.nodes())
    Y = build_Y(G, U)
    degrees = gsim.get_degrees_list(G)
    A = nx.adj_matrix(G)
    A = np.asarray(A)
    data = None
    t.start()
    data = add_feature(data, U, gsim.lp(A), G.nodes())
    print 'LP'
    data = add_feature(data, U, gsim.salton(A, degrees), G.nodes())
    print 'Salton'
    data = add_feature(data, U, gsim.jacard(A, degrees), G.nodes())
    print 'Jacard'
    data = add_feature(data, U, gsim.sorensen(A, degrees), G.nodes())
    print 'Sorensen'
    data = add_feature(data, U, gsim.hpi(A, degrees), G.nodes())
    print 'HPI'
    data = add_feature(data, U, gsim.hdi(A, degrees), G.nodes())
    print 'HDI'
    data = add_feature(data, U, gsim.lhn1(A, degrees), G.nodes())
    print 'LHN1'
    data = add_feature(data, U, gsim.pa(A, degrees), G.nodes())
    print "PA"
    t.stop()
    
    t.start()
    print 'Scaling...'
    min_max_scaler = preprocessing.MinMaxScaler()
    data = min_max_scaler.fit_transform(data)
    t.stop()
    
    t.start()
    print 'Training...'
    clf = RandomForestClassifier(n_estimators=1000)
    kfold = cross_validation.KFold(len(data), n_folds=3)
    scores = [clf.fit(data[train], Y[train]).score(data[test], Y[test]) for train, test in kfold]
    print scores
    t.stop()
    
    t.start()
    print 'Persisting'
    joblib.dump(clf, 'model_per/clf_links.pkl')
    joblib.dump(data, 'data_per/data.pkl')
    t.stop()

# @profile
def test2():
    t = Timerx(True)
    path = "/Users/rockyrock/Desktop/edges.txt"
    G = read_graph(path)
    U = build_U(G.nodes())
    Y = build_Y(G, U)
    degrees = gsim.get_degrees_list(G)
    A = nx.adj_matrix(G)
    A = np.asarray(A)
#     S = gsim.cn(A)
    data = None
    data = add_feature(data, U, gsim.cn(A), G.nodes())
      
def test1():
    t = Timerx(True)
    path = "/Users/rockyrock/Desktop/edges.txt"
    G = read_graph(path)
    U = build_U(G.nodes())
    Y = build_Y(G, U)
    print G.number_of_nodes()
    print len(U), len(Y)
    
    degrees = gsim.get_degrees_list(G)
    A = nx.adj_matrix(G)
    A = np.asarray(A)
    
    print 'cn'
    t.start()
    S = gsim.aa(A,degrees)
    t.stop()
#     i = G.nodes().index('1')
#     j = G.nodes().index('52')
#     print S[i,j]
#     print S


main()